
/**
 *
 */
package com.serviceprovider.business.module.admin.address.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.address.dao.interfaces.IGeoDao;
import com.serviceprovider.business.module.admin.address.model.Country;
import com.serviceprovider.business.module.admin.address.model.State;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

/**
 * Implementation of IGeoDao
 *
 * @author mrduongnv@gmail.com
 */
@Repository("geoDao")
public class GeoDao extends SimpleJdbcDaoSupport implements IGeoDao {
    @Autowired
    public GeoDao(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List<State> getAllStates() {
        return getSimpleJdbcTemplate().query(" SELECT * FROM state", new StateMapper());
    }

    @Override
    public List<Country> getAllCountries() {
        return getSimpleJdbcTemplate().query(" SELECT * FROM country ORDER BY name", new CountryMapper());
    }

    @Override
    public Country getCountry(String code) {
        List<Country> result = getSimpleJdbcTemplate().query("SELECT * FROM country WHERE code = ?",
                                   new CountryMapper(), code);

        return (result.size() > 0)
               ? result.get(0)
               : new Country(code);
    }

    @Override
    public State getState(String code) {
        List<State> result = getSimpleJdbcTemplate().query("SELECT * FROM state WHERE code = ?", new StateMapper(),
                                 code);

        return (result.size() > 0)
               ? result.get(0)
               : new State(code);
    }

    private class CountryMapper implements ParameterizedRowMapper<Country> {

        /**
         * map State object with db columns.
         */
        @Override
        public Country mapRow(ResultSet rs, int arg1) throws SQLException {
            Country country = new Country(rs.getString("code"));

            country.setName(rs.getString("name"));

            return country;
        }
    }


    private class StateMapper implements ParameterizedRowMapper<State> {

        /**
         * map State object with db columns.
         */
        @Override
        public State mapRow(ResultSet rs, int arg1) throws SQLException {
            State state = new State(rs.getString("code"));

            state.setName(rs.getString("name"));

            return state;
        }
    }
}



